﻿using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RabbitMQPlugin
{
    public class RabbitMQPublishHelper
    {
        /// <summary>
        /// 持久化消息发送
        /// </summary>
        public static void PublisMessage(PublishMessageModel pm)
        {
            using (var conn = new RabbitMQHelper().GetConnection())
            {
                using (var channel = conn.CreateModel())
                {
                    channel.ExchangeDeclare(pm.exchangeName, pm.exchangeType, durable: true, autoDelete: false);

                    //申明死信队列
                    channel.QueueDeclare(pm.ququeNameD, durable: true, exclusive: false, autoDelete: false, arguments: null);
                    channel.QueueBind(pm.ququeNameD, pm.exchangeNameD, pm.routingKeyD);

                    //申明普通队列
                    Dictionary<string, object> props = new Dictionary<string, object>();
                    props.Add("x-dead-letter-exchange", pm.exchangeNameD);
                    props.Add("x-dead-letter-routing-key", pm.routingKeyD);
                    channel.QueueDeclare(pm.ququeName, durable: true, exclusive: false, autoDelete: false, arguments: props);
                    channel.QueueBind(pm.ququeName, pm.exchangeName, pm.routingKey);

                    Console.WriteLine(pm.message);

                    channel.ConfirmSelect();//开启确认模式
                    channel.BasicAcks += (sender, e) =>
                    {
                        //消息到达队列回调
                        Console.WriteLine("BasicAcks消息确认收到");
                    };

                    if (pm.BasicReturn != null)
                    {
                        channel.BasicReturn += pm.BasicReturn;
                    }
                    else
                    {
                        channel.BasicReturn += (sender, e) =>
                        {
                            //消息未到达队列
                            var rCode = e.ReplyCode;
                            var rText = e.ReplyText;
                            ReadOnlySpan<byte> span = e.Body.Span;
                            string message = Encoding.UTF8.GetString(span);
                            //本地数据记录
                        };
                    }
                    
                    channel.BasicNacks += (sender, e) =>
                    {
                        //拒绝签收回调
                    };

                    IBasicProperties prop = channel.CreateBasicProperties();
                    prop.ContentType = "text/plain";
                    prop.DeliveryMode = 2;//持久化消息
                    if (!string.IsNullOrEmpty(pm.expiration))
                        prop.Expiration = pm.expiration;//过期时间
                    channel.BasicPublish(pm.exchangeName, pm.routingKey, prop, Encoding.UTF8.GetBytes(pm.message));

                    //if (channel.WaitForConfirms())
                    //{
                    //    Console.WriteLine("消息到达");
                    //}
                    //else
                    //{
                    //    //消息未到达队列
                    //    Console.WriteLine("消息发送失败");
                    //}

                }
            }
        }
    }
}
